//
//  CABaseAnimationAllViewController.m
//  iOSHeartAnimation
//
//  Created by ddy on 2018/7/26.
//  Copyright © 2018年 ddy. All rights reserved.
//

#import "CABaseAnimationAllViewController.h"
#define buttonNames @[@"位移",@"缩放",@"透明度",@"旋转",@"圆角"]
@interface CABaseAnimationAllViewController ()<CAAnimationDelegate>
/** 红色View */
@property (nonatomic, strong) UIView  *redView;
@end
static NSString * const kRedViewKey = @"kRedViewKey";
@implementation CABaseAnimationAllViewController

/** 红色View */
- (UIView *)redView{
    if(!_redView){
        _redView = [UIView new];
        _redView.backgroundColor = [UIColor redColor];
    }
    return  _redView;
}

#pragma mark --- 系统回调函数
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"AllAnimation";
    self.redView.frame = CGRectMake(150, 200, 80, 80);
    [self.view addSubview:self.redView];
    //创建按钮
    for (int i = 0 ; i < 5 ; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.tag = i;
        [btn setTitle:buttonNames[i] forState:UIControlStateNormal];
        btn.frame = CGRectMake(0, 60*i+150, 100, 50);
        //ExclusiveTouch的作用是：可以达到同一界面上多个控件接受事件时的排他性,从而避免bug。也就是说避免在一个界面上同时点击多个UIButton导致同时响应多个方法。
        btn.exclusiveTouch = YES;
        btn.backgroundColor = [UIColor blueColor];
        [btn addTarget:self action:@selector(ButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn];
    }
    
}
//按钮点击 @"位移",@"缩放",@"透明度",@"旋转",@"圆角"
- (void)ButtonClick:(UIButton *)button{
    CABasicAnimation *baseAnim = nil;
    switch (button.tag) {
        case 0:
            baseAnim = [CABasicAnimation animation];
            // byValue和toValue的区别，前者是在当前的位置上增加多少，后者是到指定的位置
            //初始化动画并设置KeyPath
            baseAnim.keyPath = @"position";
            baseAnim.byValue = [NSValue valueWithCGPoint:CGPointMake(50, 200)];
            break;
        case 1:
            baseAnim = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
            baseAnim.toValue = @(0.1f);
            break;
        case 2:
            baseAnim = [CABasicAnimation animationWithKeyPath:@"opacity"];
            baseAnim.toValue = @(0.1f);
            break;
        case 3:
            baseAnim = [CABasicAnimation animationWithKeyPath:@"transform"];
            baseAnim.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_2+M_PI_4, 1, 1, 0)];
            break;
        case 4:
            baseAnim = [CABasicAnimation animationWithKeyPath:@"cornerRadius"];
            baseAnim.toValue = @(40.0f);
            break;
            
        default:
            break;
    }
    //动画执行速度
    baseAnim.speed = 3.0f;
    //设置代理
    baseAnim.delegate = self;
    //重复次数
    baseAnim.repeatCount = 2;
    //动画节奏
    baseAnim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    //图层是否显示执行后的动画执行后的位置以及形态
    baseAnim.removedOnCompletion = NO;
    baseAnim.fillMode = kCAFillModeForwards;
    //动画完成后是否以动画的形式回到初始值
    baseAnim.autoreverses = YES;
    //动画时间
    baseAnim.duration = 2.0f;
    [self.redView.layer addAnimation:baseAnim forKey:kRedViewKey];
}

#pragma mark --- <CAAnimationDelegate>
- (void)animationDidStart:(CAAnimation *)anim{
    if ([self.redView.layer animationForKey:kRedViewKey] == anim) {
        NSLog(@"red View start");
    }
}
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    if ([self.redView.layer animationForKey:kRedViewKey] == anim) {
        NSLog(@"red View stop");
    }
}





@end
